using System;
using Reflector;
using DeblectorAddIn.WorkItems;
using CompositeUIExtensions.Hosting;
using System.Windows.Forms;


namespace DeblectorAddIn
{
    public class EntryPoint :UserControlShellApplication<ContentPanel>,IPackage
    {
        private IWindowManager _windowManager;
        private ICommandBarManager _commandBarManager;
        private ICommandBarSeparator _separator;
        private ICommandBarButton _button;
        private ToolbarButtons _toolbarItems;
        private ICommandBarMenu _debugMenu;
        private DebuggerWorkItem _dwi;
        UserControlHost<EntryPoint, ContentPanel> _plugin;
        public EntryPoint()
        {
            _toolbarItems = new ToolbarButtons();  
        }
        #region IPackage Members
        
        public void Load(IServiceProvider serviceProvider)
        {
            _plugin = new UserControlHost<EntryPoint, ContentPanel>();
            var root = new RootWorkItem();
            _plugin.RootWorkItem.WorkItems.Add(root,"main");
            _plugin.Dock = DockStyle.Fill;
            _windowManager = (IWindowManager)serviceProvider.GetService(typeof(IWindowManager));
            _windowManager.Windows.Add("Deblector", _plugin, "Deblector");
            _commandBarManager = (ICommandBarManager)serviceProvider.GetService(typeof(ICommandBarManager));
            _separator = _commandBarManager.CommandBars["Tools"].Items.AddSeparator();
            _button = _commandBarManager.CommandBars["Tools"].Items.AddButton("Deblector", new EventHandler(this.DeblectorButton_Click));
            _button.Image = _plugin.UserControlShell.Images.Images[0];
            root.Services.Add<IServiceProvider>(serviceProvider);
            root.Services.Add<FindFrameInAssemblyBrowser>(new FindFrameInAssemblyBrowser());
            root.Run();
        }
        private void RegisterDebugMenu()
        {
            RootWorkItem root = (RootWorkItem)_plugin.RootWorkItem.WorkItems["main"];
            var deblectorButton = _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Show Deblector", _plugin.UserControlShell.Images.Images[0], DeblectorButton_Click, Keys.F6);
            _toolbarItems.Add("Deblector", deblectorButton);
            _toolbarItems.Add("", _commandBarManager.CommandBars["ToolBar"].Items.AddSeparator());
            _toolbarItems.Add("go", _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Run/Continue", _plugin.UserControlShell.Images.Images[2], new EventHandler(root.Run_Click), Keys.F7));
            _toolbarItems.Add("stop", _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Stop", _plugin.UserControlShell.Images.Images[1], new EventHandler(root.Stop_Click)));
            _toolbarItems.Add("break", _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Break All", _plugin.UserControlShell.Images.Images[3], new EventHandler(root.Break_Click)));
            _toolbarItems.Add("stepinto", _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Step Into", _plugin.UserControlShell.Images.Images[4], new EventHandler(root.StepInto_Click), Keys.F11));
            _toolbarItems.Add("stepout", _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Step Out", _plugin.UserControlShell.Images.Images[5], new EventHandler(root.StepOut_Click)));
            _toolbarItems.Add("stepover", _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Step Over", _plugin.UserControlShell.Images.Images[6], new EventHandler(root.StepOver_Click), Keys.F10));
            _toolbarItems.Add("attach", _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Attach to process", _plugin.UserControlShell.Images.Images[7], new EventHandler(root.AttachProcess_Click)));
            _toolbarItems.Add("breakpoint", _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Break", _plugin.UserControlShell.Images.Images[8], new EventHandler(root.ToggleBreak_Click),Keys.F9));
            _toolbarItems.Add("breakonexception", _commandBarManager.CommandBars["ToolBar"].Items.AddButton("Break On Exceptions", _plugin.UserControlShell.Images.Images[9], new EventHandler(root.BreakOnException_Click)));
            int nCount = _commandBarManager.CommandBars["MenuBar"].Items.Count;
            _debugMenu = _commandBarManager.CommandBars["MenuBar"].Items.InsertMenu(nCount - 2, "Debug", "&Debug");
            _debugMenu.Items.Add(_toolbarItems["go"]);
            _debugMenu.Items.Add(_toolbarItems["stop"]);
            _debugMenu.Items.Add(_toolbarItems["break"]);
            _debugMenu.Items.Add(_toolbarItems["stepinto"]);
            _debugMenu.Items.Add(_toolbarItems["stepout"]);
            _debugMenu.Items.Add(_toolbarItems["stepover"]);
            _debugMenu.Items.Add(_toolbarItems["attach"]);
            _debugMenu.Items.Add(_toolbarItems["breakpoint"]);
            _debugMenu.Items.Add(_toolbarItems["breakonexception"]);
            foreach (ICommandBarItem item in _toolbarItems.Values)
                item.Enabled = false;
            deblectorButton.Enabled = true;
            root.Services.Add<ICommandBarMenu>(_debugMenu);
            root.Services.Add<ToolbarButtons>(_toolbarItems);
        }
        public void Unload()
        {
            _windowManager.Windows.Remove("Deblector");
            _commandBarManager.CommandBars["Tools"].Items.Remove(_button);
            _commandBarManager.CommandBars["Tools"].Items.Remove(_separator);
            foreach (ICommandBarItem ici in _toolbarItems.Values)
                _commandBarManager.CommandBars["ToolBar"].Items.Remove(ici);
            _commandBarManager.CommandBars["MenuBar"].Items.Remove(_debugMenu);
        }

        #endregion
        private void DeblectorButton_Click(object sender, EventArgs e)
        {
            _windowManager.Windows["Deblector"].Visible = true;
            _windowManager.Windows["Deblector"].Content.Focus();
            if (null == _dwi)
            {
                RegisterDebugMenu();
                //System.Diagnostics.Debug.Assert(false);
                _dwi = new DebuggerWorkItem();
                _plugin.RootWorkItem.WorkItems["main"].WorkItems.Add(_dwi,"debugger");
                _dwi.Run();
            }
            
        }
       
        
    }
}
